Before embarking on the exciting journey of writing
your first program, it's crucial to grasp the language you'll be using, just
like a chef familiarizes themselves with the ingredients before creating a mouth-watering
dish. Understanding the language will not only make the programming process
smoother but will also enable you to express your creativity in a more
meaningful and impactful way.
What is C language?
It is a general
purpose programming language created by Dennis Ritche at the AT&T Bell
laboratories in 1972. Dennis Ritchie and Thompson used C language to develop the UNIX
operating system.
C gives us insight
into how the hardware and software interact with each other.
Most compilers,
kernels and OS are written in C.C is not an object oriented language but its
superset C++ is an
object-oriented programming (OPP) language.
Getting Started
The two major prerequisites required are; A text editor like vim
or vs code and a compiler like GCC, to translate the code into a language the
computer will understand.
What is a text editor?
A text editor is a
software program that is used to create, edit and manipulate plain text files. Unlike word
processors, which are designed for creating formatted documents, text editors are optimized for
working with unformatted, plain text files. Text editors are commonly used by programmers, web
developers, and markup languages. They are typically lightweight and fast and often include
features such as syntax highlighting, autocompletion, and code folding, which
can help improve productivity and reduce errors.
Some popular text
editors include Notepad, Sublime Text, Visual Studio Code, and Vim.
What is a compiler?
A compiler is a software tool that translates
source code written in a high-level programming language into machine code that can be executed directly by a computer’s processor. The compiler takes the source code, which is in a human-readable format,
and converts it into machine code which is a binary
representation of the instructions that the computer can execute.
“Hello, World!” program in C
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Open a text editor
and create a file named hello.c.
For example type the
command vi hello.c in the command line interface in order to access the
hello.c file in the vi editor.
Press the I key
in order to enter insert mode and then key in the source code as follows:
#include <stdio.h>
In order to use functions such as the printf(); function in our program we need to include
the stdio.h header file
using #include <stdio.h> statement.
int main()
All valid C programs must contain int
main() function. This is the starting point from where the execution
begins.
printf();
The printf(); is a library function
to send formatted output to the screen. The function prints the string inside
the quotations ("Hello,
World!").
return 0;
return 0; Marks the end of the function. It is the exit status inside the main function.
\n
\n is an escape character that means new line.
After finishing the
code press the esc button to exit insert mode then type the following commands
to save and exit the
editor :wq
Once in the command line
type gcc hello.c to compile the c file. When you get no response that means the code has been compiled
successfully but if the code does not compile properly you will get some errors. When you
compile a file it creates a new file that we can run to run the file we type ./a.out It
will give us the output.
Hello, World!
Congratulations on writing your
first C program! Now that you've dipped your toes into the world of programming, why not take it a
step further and experiment with manipulating the string to unleash your inner creativity? By tweaking the string, you
can unlock a world of endless possibilities and produce a diverse range of outputs that are
uniquely yours. So go ahead and explore the power of programming - who knows what amazing things you'll
discover along the way!
Comments
Post a Comment